0.2.0 • Published 4 years ago

prescript-webpack-plugin v0.2.0

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

prescript-webpack-plugin

Prescript is intended for those rare cases where you want to run scripts before transpiling/bundling your code with webpack.

A typical example usage is to auto-generate e.g typescript typings based on often changing json files like language files. On every run of webpack the scripts will be run before any transpiling or bundling takes place.

Installing

npm install prescript-webpack-plugin

Usage

const PrescriptWebpackPlugin = require('prescript-webpack-plugin');

Add the plugin to your webpack plugins section:

plugins: [
    new PrescriptWebpackPlugin({
        // Scripts are executed in the order they are added
        // The different types of scripts are listed further down in the documentation
        scripts: [
            {
                type: 'node',
                args: ['arg1'],
                scriptFile: path.join(__dirname, 'prescript.js')
            },
            {
                type: 'node',
                args: [1, 'param2'],
                script: (param1, param2) => {
                    console.log(param1, param2);
                }
            }
        ]
    })
];

Prescript types

node

Node scripts are plain javascript that can be executed either as an inline script or as a separate process using a .js file as input.

PropertyDescription
typeMust be set to 'node'
interpreterOptional. Specify an alternative node executable if required when using scriptFile
scriptFileAbsolute path to script file that should be run, can't be combined with script
scriptA function that should be run, can't be combined with scriptFile
argsOptional. Must be strings when using scriptFile
throwOnErrorIf a build should fail on an error or continue silently
workingDirectoryThe working directory used when executing scriptFile, defaults to webpack's

Examples

Using scriptFile:

{
    type: 'node',
    args: ['arg1'],
    scriptFile: path.join(__dirname, 'prescript.js'),
    interpreter: '/some/path/to/node',
    workingDirectory: '/work/dir/for/node',
    throwOnError: true
}

Using script:

{
    type: 'node',
    args: [1, 'param2'],
    script: (param1, param2) => {
        console.log(param1, param2);
    }
}

ts-node

Similar to type: 'node' but executes a script file using ts-node instead which allows you to use TypeScript.

PropertyDescription
typeMust be set to 'ts-node'
interpreterOptional. Specify an alternative ts-node executable if required
argsOptional. Must be strings
throwOnErrorIf a build should fail on an error or continue silently
workingDirectoryThe working directory used when executing scriptFile, defaults to webpack's

Examples

Using scriptFile:

{
    type: 'ts-node',
    args: ['arg1'],
    scriptFile: path.join(__dirname, 'prescript.ts'),
    interpreter: '/some/path/to/ts-node',
    workingDirectory: '/work/dir/for/ts-node',
    throwOnError: true
},

shell

Run any shell command, this could for example enable you to run python scripts or anything else that could be needed.

PropertyDescription
typeMust be set to 'shell'
commandThe shell command you want to execute
argsOptional. Must be strings
throwOnErrorIf a build should fail on an error or continue silently
workingDirectoryThe working directory used when executing command, defaults to webpack's

Examples

{
    type: 'shell',
    command: 'echo',
    args: ['hello', 'world'],
    workingDirectory: '/work/dir/for/command',
    throwOnError: true
},

Common issues

Since prescripts are executed just before webpack's build triggers it can cause issues when modifying files that webpack works with during building in combination with webpack's watchers. To accomodate this, you can tweak the amount milliseconds that need to elapse before executing prescripts again.

plugins: [
    new PrescriptWebpackPlugin({
        millisecondsBetweenRuns: 500, // 500 ms is the default
        scripts: [
            /* ... */
        ]
    })
];
0.2.0

4 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

6 years ago

0.1.0

6 years ago