Skip to content

Commit ad48f1c

Browse files
authored
Emit a deprecation warning when loaded as a default export (#230)
See #229
1 parent 4e12a1a commit ad48f1c

File tree

6 files changed

+195
-6
lines changed

6 files changed

+195
-6
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ jobs:
5252
with:
5353
node-version: ${{ matrix.node-version }}
5454
check-latest: true
55-
- uses: frenck/action-setup-yq@v1
56-
with: {version: v4.30.5} # frenck/action-setup-yq#35
5755
- uses: dart-lang/setup-dart@v1
5856
with: {sdk: stable}
5957
- run: dart --version
@@ -72,6 +70,8 @@ jobs:
7270
npm run init -- --compiler-path=dart-sass --language-path=language $args
7371
7472
- run: npm run test
73+
- run: npm run compile
74+
- run: node test/after-compile-test.mjs
7575

7676
# The versions should be kept up-to-date with the latest LTS Node releases.
7777
# They next need to be rotated October 2021. See
@@ -98,8 +98,6 @@ jobs:
9898
with: {sdk: stable}
9999
- uses: actions/setup-node@v2
100100
with: {node-version: "${{ matrix.node_version }}"}
101-
- uses: frenck/action-setup-yq@v1
102-
with: {version: v4.30.5} # frenck/action-setup-yq#35
103101

104102
- name: Check out Dart Sass
105103
uses: sass/clone-linked-repo@v1

lib/index.mjs

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import * as sass from './index.js';
2+
3+
export const compile = sass.compile;
4+
export const compileAsync = sass.compileAsync;
5+
export const compileString = sass.compileString;
6+
export const compileStringAsync = sass.compileStringAsync;
7+
export const Logger = sass.Logger;
8+
export const SassArgumentList = sass.SassArgumentList;
9+
export const SassBoolean = sass.SassBoolean;
10+
export const SassColor = sass.SassColor;
11+
export const SassFunction = sass.SassFunction;
12+
export const SassList = sass.SassList;
13+
export const SassMap = sass.SassMap;
14+
export const SassNumber = sass.SassNumber;
15+
export const SassString = sass.SassString;
16+
export const Value = sass.Value;
17+
export const CustomFunction = sass.CustomFunction;
18+
export const ListSeparator = sass.ListSeparator;
19+
export const sassFalse = sass.sassFalse;
20+
export const sassNull = sass.sassNull;
21+
export const sassTrue = sass.sassTrue;
22+
export const Exception = sass.Exception;
23+
export const PromiseOr = sass.PromiseOr;
24+
export const info = sass.info;
25+
export const render = sass.render;
26+
export const renderSync = sass.renderSync;
27+
export const TRUE = sass.TRUE;
28+
export const FALSE = sass.FALSE;
29+
export const NULL = sass.NULL;
30+
export const types = sass.types;
31+
32+
let printedDefaultExportDeprecation = false;
33+
function defaultExportDeprecation() {
34+
if (printedDefaultExportDeprecation) return;
35+
printedDefaultExportDeprecation = true;
36+
console.error(
37+
"`import sass from 'sass'` is deprecated.\n" +
38+
"Please use `import * as sass from 'sass'` instead.");
39+
}
40+
41+
export default {
42+
get compile() {
43+
defaultExportDeprecation();
44+
return sass.compile;
45+
},
46+
get compileAsync() {
47+
defaultExportDeprecation();
48+
return sass.compileAsync;
49+
},
50+
get compileString() {
51+
defaultExportDeprecation();
52+
return sass.compileString;
53+
},
54+
get compileStringAsync() {
55+
defaultExportDeprecation();
56+
return sass.compileStringAsync;
57+
},
58+
get Logger() {
59+
defaultExportDeprecation();
60+
return sass.Logger;
61+
},
62+
get SassArgumentList() {
63+
defaultExportDeprecation();
64+
return sass.SassArgumentList;
65+
},
66+
get SassBoolean() {
67+
defaultExportDeprecation();
68+
return sass.SassBoolean;
69+
},
70+
get SassColor() {
71+
defaultExportDeprecation();
72+
return sass.SassColor;
73+
},
74+
get SassFunction() {
75+
defaultExportDeprecation();
76+
return sass.SassFunction;
77+
},
78+
get SassList() {
79+
defaultExportDeprecation();
80+
return sass.SassList;
81+
},
82+
get SassMap() {
83+
defaultExportDeprecation();
84+
return sass.SassMap;
85+
},
86+
get SassNumber() {
87+
defaultExportDeprecation();
88+
return sass.SassNumber;
89+
},
90+
get SassString() {
91+
defaultExportDeprecation();
92+
return sass.SassString;
93+
},
94+
get Value() {
95+
defaultExportDeprecation();
96+
return sass.Value;
97+
},
98+
get CustomFunction() {
99+
defaultExportDeprecation();
100+
return sass.CustomFunction;
101+
},
102+
get ListSeparator() {
103+
defaultExportDeprecation();
104+
return sass.ListSeparator;
105+
},
106+
get sassFalse() {
107+
defaultExportDeprecation();
108+
return sass.sassFalse;
109+
},
110+
get sassNull() {
111+
defaultExportDeprecation();
112+
return sass.sassNull;
113+
},
114+
get sassTrue() {
115+
defaultExportDeprecation();
116+
return sass.sassTrue;
117+
},
118+
get Exception() {
119+
defaultExportDeprecation();
120+
return sass.Exception;
121+
},
122+
get PromiseOr() {
123+
defaultExportDeprecation();
124+
return sass.PromiseOr;
125+
},
126+
get info() {
127+
defaultExportDeprecation();
128+
return sass.info;
129+
},
130+
get render() {
131+
defaultExportDeprecation();
132+
return sass.render;
133+
},
134+
get renderSync() {
135+
defaultExportDeprecation();
136+
return sass.renderSync;
137+
},
138+
get TRUE() {
139+
defaultExportDeprecation();
140+
return sass.TRUE;
141+
},
142+
get FALSE() {
143+
defaultExportDeprecation();
144+
return sass.FALSE;
145+
},
146+
get NULL() {
147+
defaultExportDeprecation();
148+
return sass.NULL;
149+
},
150+
get types() {
151+
defaultExportDeprecation();
152+
return sass.types;
153+
},
154+
};

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
"repository": "sass/embedded-host-node",
88
"author": "Google Inc.",
99
"license": "MIT",
10+
"exports": {
11+
"import": "dist/lib/index.mjs",
12+
"default": "dist/lib/index.js"
13+
},
1014
"main": "dist/lib/index.js",
1115
"types": "dist/types/index.d.ts",
1216
"files": [
@@ -21,7 +25,7 @@
2125
"check:gts": "gts check",
2226
"check:tsc": "tsc --noEmit",
2327
"clean": "gts clean",
24-
"compile": "tsc",
28+
"compile": "tsc && cp lib/index.mjs dist/lib/index.mjs",
2529
"fix": "gts fix",
2630
"prepublishOnly": "npm run clean && ts-node ./tool/prepare-release.ts",
2731
"test": "jest"

test/after-compile-test.mjs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2023 Google LLC. Use of this source code is governed by an
2+
// MIT-style license that can be found in the LICENSE file or at
3+
// https://opensource.org/licenses/MIT.
4+
5+
import * as fs from 'fs';
6+
7+
// Note: this file isn't .test.ts specifically because we _don't_ want Jest to
8+
// handle it, because Jest chokes on dynamic imports of literal ESM modules.
9+
10+
// This file should only be run _after_ `npm run compile`.
11+
if (!fs.existsSync('dist/package.json')) {
12+
throw new Error('after-compile.test.ts must be run after `npm run compile`.');
13+
}
14+
15+
// Load these dynamically so we have a better error mesage if `npm run compile`
16+
// hasn't been run.
17+
const cjs = await import('../dist/lib/index.js');
18+
const esm = await import('../dist/lib/index.mjs');
19+
20+
for (const [name, value] of Object.entries(cjs)) {
21+
if (name === '__esModule' || name === 'default') continue;
22+
if (!esm[name]) {
23+
throw new Error(`ESM module is missing export ${name}.`);
24+
} else if (esm[name] !== value) {
25+
throw new Error(`ESM ${name} isn't the same as CJS.`);
26+
}
27+
28+
if (!esm.default[name]) {
29+
throw new Error(`ESM default export is missing export ${name}.`);
30+
} else if (esm.default[name] !== value) {
31+
throw new Error(`ESM default export ${name} isn't the same as CJS.`);
32+
}
33+
}

test/dependencies.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// MIT-style license that can be found in the LICENSE file or at
33
// https://opensource.org/licenses/MIT.
44

5-
import * as child_process from 'child_process';
65
import * as fs from 'fs';
76
import * as p from 'path';
87

tool/prepare-release.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {getLanguageRepo} from './get-language-repo';
1616

1717
console.log('Transpiling TS into dist.');
1818
shell.exec('tsc');
19+
shell.cp('lib/index.mjs', 'dist/lib/index.mjs');
1920

2021
console.log('Copying JS API types to dist.');
2122
shell.cp('-R', 'lib/src/vendor/sass', 'dist/types');

0 commit comments

Comments
 (0)