-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle process.env correctly in build #2267
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,23 +55,16 @@ function runTypeScriptBuild(outDir, version, target, declarations) { | |
} | ||
} | ||
|
||
async function generateBundledModule(inputPath, version, outputFile, format, env = "development") { | ||
async function generateBundledModule(inputPath, version, outputFile, format, env) { | ||
console.log(`Generating ${version} ${outputFile} ${env} bundle.`) | ||
|
||
const replaceEnv = env && replacePlugin({ "process.env.NODE_ENV": JSON.stringify(env) }) | ||
|
||
let plugins | ||
if (env === "production") { | ||
plugins = [ | ||
resolvePlugin(), | ||
replacePlugin({ "process.env.NODE_ENV": JSON.stringify("production") }), | ||
terserPlugin(), | ||
filesizePlugin() | ||
] | ||
plugins = [resolvePlugin(), replaceEnv, terserPlugin(), filesizePlugin()] | ||
} else { | ||
plugins = [ | ||
resolvePlugin(), | ||
replacePlugin({ "process.env.NODE_ENV": JSON.stringify(env) }), | ||
filesizePlugin() | ||
] | ||
plugins = [resolvePlugin(), replaceEnv, filesizePlugin()] | ||
} | ||
|
||
const bundle = await rollup({ | ||
|
@@ -104,7 +97,7 @@ const pkg = require("../package.json") | |
function writePackage(versionPath, version) { | ||
// replace `0.y.z` with `v.y.z`, strip `v` prefix | ||
const pkgVersion = pkg.version.replace(/^0/, version.replace("v", "")) | ||
return fs.writeFile( | ||
fs.writeFileSync( | ||
path.resolve(versionPath, "package.json"), | ||
JSON.stringify( | ||
{ | ||
|
@@ -121,6 +114,19 @@ function writePackage(versionPath, version) { | |
) | ||
} | ||
|
||
function writeIndex(versionPath) { | ||
fs.writeFileSync( | ||
path.resolve(versionPath, "lib", "index.js"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not critical, but IMHO it's better to write this file somewhere in the codebase and just copy it as part of the build process. It will make sure that we're not making any mistake and makes the code a bit clearer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea, was thinking about it but did not want to come up with some artificial filename or folder where to keep it. It's such a small thing it's ok. |
||
` | ||
if (typeof process !== 'undefined' && process.env.NODE_ENV === 'production') { | ||
module.exports = require('./mobx.min.js'); | ||
} else { | ||
module.exports = require('./mobx.js'); | ||
} | ||
` | ||
) | ||
} | ||
|
||
async function build(version) { | ||
const distPath = "dist" | ||
const es5Build = path.join(distPath, ".build.es5") | ||
|
@@ -131,18 +137,20 @@ async function build(version) { | |
runTypeScriptBuild(es6Build, version, ts.ScriptTarget.ES2015, false) | ||
|
||
await Promise.all([ | ||
generateBundledModule(es5Build, version, "mobx.js", "cjs"), | ||
generateBundledModule(es5Build, version, "mobx.js", "cjs", "development"), | ||
generateBundledModule(es5Build, version, "mobx.min.js", "cjs", "production"), | ||
|
||
generateBundledModule(es5Build, version, "mobx.module.js", "es"), | ||
generateBundledModule(es6Build, version, "mobx.es6.js", "es"), | ||
|
||
generateBundledModule(es5Build, version, "mobx.umd.js", "umd"), | ||
generateBundledModule(es5Build, version, "mobx.umd.min.js", "umd", "production") | ||
generateBundledModule(es5Build, version, "mobx.umd.min.js", "umd", "production"), | ||
|
||
copyAssets(versionPath) | ||
]) | ||
|
||
await copyAssets(versionPath) | ||
await writePackage(versionPath, version) | ||
writeIndex(versionPath) | ||
writePackage(versionPath, version) | ||
} | ||
|
||
Promise.all([build("v4"), build("v5")]).catch(e => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the same as
"small-build"
I would remove one of themThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to standardize to
build
but not to removesmall-build
in case someone still uses that for something.