Skip to content

Commit 1d9d3c4

Browse files
committed
Require Node.js 18 and move to ESM
1 parent 8446a19 commit 1d9d3c4

File tree

9 files changed

+87
-92
lines changed

9 files changed

+87
-92
lines changed

.gitattributes

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
* text=auto
2-
*.js text eol=lf
1+
* text=auto eol=lf

.github/workflows/main.yml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13-
- 14
14-
- 12
15-
- 10
16-
- 8
17-
- 6
13+
- 20
14+
- 18
1815
steps:
19-
- uses: actions/checkout@v2
20-
- uses: actions/setup-node@v1
16+
- uses: actions/checkout@v4
17+
- uses: actions/setup-node@v4
2118
with:
2219
node-version: ${{ matrix.node-version }}
2320
- run: npm install

fixture/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
// eslint-disable-next-line n/prefer-global/process
12
console.log(process.env.NODE_ENV);

fixture/webpack.config.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
'use strict';
2-
const assert = require('assert');
3-
const NodeEnvPlugin = require('..');
1+
import process from 'node:process';
2+
import assert from 'node:assert';
3+
import {fileURLToPath} from 'node:url';
4+
import path from 'node:path';
5+
import NodeEnvPlugin from '../index.js';
6+
7+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
48

59
assert.strictEqual(process.env.NODE_ENV, 'development');
610

7-
module.exports = {
11+
const config = {
812
output: {
9-
filename: 'unicorn.js'
13+
filename: 'unicorn.js',
1014
},
1115
entry: __dirname,
1216
plugins: [
13-
new NodeEnvPlugin()
14-
]
17+
new NodeEnvPlugin(),
18+
],
1519
};
20+
21+
export default config;

index.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
'use strict';
2-
const webpack = require('webpack');
3-
const hasFlag = require('has-flag');
1+
import process from 'node:process';
2+
import webpack from 'webpack';
3+
import hasFlag from 'has-flag';
44

5-
if (!process.env.NODE_ENV) {
6-
process.env.NODE_ENV = hasFlag('p') ? 'production' : 'development';
7-
}
5+
process.env.NODE_ENV ||= hasFlag('p') ? 'production' : 'development';
86

9-
module.exports = class NodeEnvPlugin {
7+
export default class NodeEnvPlugin {
108
apply(compiler) {
11-
compiler.apply(new webpack.DefinePlugin({
12-
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
13-
}));
9+
new webpack.DefinePlugin({
10+
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
11+
}).apply(compiler);
1412
}
15-
};
13+
}
1614

17-
module.exports.isProduction = process.env.NODE_ENV === 'production';
18-
module.exports.isDevelopment = process.env.NODE_ENV === 'development';
19-
module.exports.isTest = process.env.NODE_ENV === 'test';
20-
module.exports.devtool = module.exports.isProduction ? 'source-map' : 'cheap-module-source-map';
15+
export const isProduction = process.env.NODE_ENV === 'production';
16+
export const isDevelopment = process.env.NODE_ENV === 'development';
17+
export const isTest = process.env.NODE_ENV === 'test';
18+
export const devtool = isProduction ? 'source-map' : 'cheap-module-source-map';

license

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
3+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
66

package.json

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,45 @@
11
{
2-
"name": "node-env-webpack-plugin",
3-
"version": "1.1.0",
4-
"description": "Simplified `NODE_ENV` handling with webpack",
5-
"license": "MIT",
6-
"repository": "sindresorhus/node-env-webpack-plugin",
7-
"author": {
8-
"name": "Sindre Sorhus",
9-
"email": "sindresorhus@gmail.com",
10-
"url": "sindresorhus.com"
11-
},
12-
"engines": {
13-
"node": ">=6"
14-
},
15-
"scripts": {
16-
"test": "xo && ava"
17-
},
18-
"files": [
19-
"index.js"
20-
],
21-
"keywords": [
22-
"webpack-plugin",
23-
"webpack",
24-
"node",
25-
"node-env",
26-
"env",
27-
"environment",
28-
"development",
29-
"production"
30-
],
31-
"dependencies": {
32-
"has-flag": "^3.0.0"
33-
},
34-
"devDependencies": {
35-
"ava": "*",
36-
"pify": "^3.0.0",
37-
"tempy": "^0.2.1",
38-
"webpack": "^3.10.0",
39-
"xo": "*"
40-
}
2+
"name": "node-env-webpack-plugin",
3+
"version": "1.1.0",
4+
"description": "Simplified `NODE_ENV` handling with webpack",
5+
"license": "MIT",
6+
"repository": "sindresorhus/node-env-webpack-plugin",
7+
"funding": "https://github.com/sponsors/sindresorhus",
8+
"author": {
9+
"name": "Sindre Sorhus",
10+
"email": "sindresorhus@gmail.com",
11+
"url": "https://sindresorhus.com"
12+
},
13+
"type": "module",
14+
"exports": "./index.js",
15+
"sideEffects": false,
16+
"engines": {
17+
"node": ">=18"
18+
},
19+
"scripts": {
20+
"test": "xo && ava"
21+
},
22+
"files": [
23+
"index.js"
24+
],
25+
"keywords": [
26+
"webpack-plugin",
27+
"webpack",
28+
"node",
29+
"node-env",
30+
"env",
31+
"environment",
32+
"development",
33+
"production"
34+
],
35+
"dependencies": {
36+
"has-flag": "^5.0.1"
37+
},
38+
"devDependencies": {
39+
"ava": "^6.1.2",
40+
"pify": "^6.1.0",
41+
"tempy": "^3.1.0",
42+
"webpack": "^5.91.0",
43+
"xo": "^0.58.0"
44+
}
4145
}

readme.md

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@
22

33
> Simplified `NODE_ENV` handling with [webpack](https://webpack.js.org)
44
5-
65
## Install
76

8-
```
9-
$ npm install node-env-webpack-plugin
7+
```sh
8+
npm install node-env-webpack-plugin
109
```
1110

12-
1311
## Usage
1412

1513
```diff
16-
'use strict';
17-
const path = require('path');
18-
+ const NodeEnvPlugin = require('node-env-webpack-plugin');
14+
import path from 'node:path';
15+
+ import NodeEnvPlugin from 'node-env-webpack-plugin';
1916

2017
- const NODE_ENV = process.env.NODE_ENV || 'development';
2118
- const isProduction = NODE_ENV === 'production';
@@ -46,7 +43,6 @@ $ npm install node-env-webpack-plugin
4643
};
4744
```
4845

49-
5046
## API
5147

5248
Sets `process.env.NODE_ENV` in the Node.js process to `development` at import-time if it's not defined, or `production` if webpack is run with `webpack -p`.
@@ -73,13 +69,6 @@ For convenience and to prevent typos.
7369

7470
Pass this to the webpack `devtool` option. It will give you good but slow source maps in production (`source-map`) and fast source maps in development (`cheap-module-source-map`).
7571

76-
7772
## Related
7873

7974
- [add-asset-webpack-plugin](https://github.com/sindresorhus/add-asset-webpack-plugin) - Dynamically add an asset to the webpack graph
80-
- [add-module-exports-webpack-plugin](https://github.com/sindresorhus/add-module-exports-webpack-plugin) - Add `module.exports` for Babel and TypeScript compiled code
81-
82-
83-
## License
84-
85-
MIT © [Sindre Sorhus](https://sindresorhus.com)

test.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import fs from 'fs';
2-
import path from 'path';
1+
import process from 'node:process';
2+
import fs from 'node:fs';
3+
import path from 'node:path';
34
import test from 'ava';
45
import webpack from 'webpack';
5-
import tempy from 'tempy';
6+
import {temporaryDirectory} from 'tempy';
67
import pify from 'pify';
78

89
delete process.env.NODE_ENV;
910

10-
test(async t => {
11-
const config = require('./fixture/webpack.config');
12-
const cwd = tempy.directory();
11+
test('main', async t => {
12+
const {default: config} = await import('./fixture/webpack.config.js');
13+
const cwd = temporaryDirectory();
1314
config.output.path = cwd;
1415
await pify(webpack)(config);
1516
t.regex(fs.readFileSync(path.join(cwd, 'unicorn.js'), 'utf8'), /development/);

0 commit comments

Comments
 (0)