Jak usunąć globalne "use strict" dodane przez babel

Używam formy funkcji "use strict" i nie chcę globalnej formy, którą Babel dodaje po transpilacji. Problem polega na tym, że używam niektórych bibliotek, które nie używają trybu "użyj ścisłego" i może to spowodować błąd po połączeniu skryptów

Author: ani h, 2015-11-20

16 answers

Babel 5

Czarna lista "useStrict". Na przykład oto przykład w pliku Gruntfile:

babel: {
    options: {
        blacklist: ["useStrict"],
        // ...
    },
    // ...
}

Babel 6

Ponieważ Babel 6 jest w pełni opt-in dla wtyczek teraz , zamiast czarnej listy useStrict, po prostu nie zawierają strict-mode plugin . Jeśli używasz presetu, który go zawiera, myślę, że będziesz musiał stworzyć swój własny, który zawiera wszystkie inne, ale nie ten.

 27
Author: T.J. Crowder,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-12 17:06:57

Jak już wspomniano o Babel 6, jest to transform-es2015-modules-commonjs preset, który dodaje tryb ścisły. Jeśli chcesz użyć całego Ustawienia es2015 bez przekształceń modułu, umieść to w pliku .babelrc:

{
  "presets": [
    ["es2015", { "modules": false }]
  ]
}

Spowoduje to wyłączenie modułów i trybu ścisłego, przy jednoczesnym włączeniu wszystkich innych przekształceń es2015.

 46
Author: rcode,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-25 01:40:33

Jest teraz wtyczka babel, którą możesz dodać do konfiguracji, która usunie tryb ścisły: babel-plugin-transform-remove-strict-mode. Jest trochę brzydki w tym, że "use strict" jest dodawany, a następnie usuwany, ale to sprawia, że config jest o wiele ładniejszy.

Dokumenty są w repo GitHub: https://github.com/genify/babel-plugin-transform-remove-strict-mode

Twoje .babelrc wygląda tak:
{
  "presets": ["env"],
  "plugins": ["transform-remove-strict-mode"]
}
 13
Author: Alan Pierce,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-26 03:29:03

Doszedłem również do tego dość absurdalnego ograniczenia, że nie można wyłączyć lub nadpisać ustawień z istniejącego predefiniowanego, i uciekłem się do używania tego predefiniowanego: https://www.npmjs.com/package/babel-preset-es2015-without-strict

 8
Author: Adam Reis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-21 17:50:04
plugins: [
    [
        require("@babel/plugin-transform-modules-commonjs"), 
        {
            strictMode: false
        }
    ],
]
 6
Author: Kley Andromorfia,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-05-31 12:41:18

Babel 6 + es2015

Możemy wyłączyć babel-plugin-transform-es2015-modules-commonjs, aby wymagać babel-plugin-transform-strict-mode.

Więc skomentuj poniższy kod w node_modules/babel-plugin-transform-es2015-modules-commonjs/lib/index.js w linii 151

//inherits: require("babel-plugin-transform-strict-mode"),
 5
Author: shihongzhi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-23 17:12:38

Osobiście używam wtyczki gulp-iife i owijam IIFEs wokół wszystkich moich plików. Zauważyłem, że wtyczka babel (używając preset es2015) dodaje również globalny "use strict". Uruchamiam mój kod pocztowy babel przez wtyczkę iife stream ponownie, więc anuluje to, co zrobił babel.

gulp.task("build-js-source-dev", function () {
	return gulp.src(jsSourceGlob)
      .pipe(iife())
	  .pipe(plumber())
	  .pipe(babel({ presets: ["es2015"] }))// compile ES6 to ES5
	  .pipe(plumber.stop())
      .pipe(iife()) // because babel preset "es2015" adds a global "use strict"; which we dont want
      .pipe(concat(jsDistFile)) // concat to single file
	  .pipe(gulp.dest("public_dist"))
});
 4
Author: Brian Schermerhorn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-28 02:00:31

Just change .babelrc solution

Jeśli nie chcesz zmieniać żadnych modułów npm, możesz użyć .babelrc ignoruj w ten sposób

{
  "presets": ["es2015"],
  "ignore": [
    "./src/js/directive/datePicker.js"
  ]
}

Zignoruj ten plik, działa na mnie!

Ignorowany plik, który nie może użyć 'use strict' jest starym kodem i nie trzeba używać babel do jego transformacji!

 4
Author: hisland,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-13 01:15:31

Możesz powiedzieć babelowi, że Twój kod jest skryptem z:

sourceType: "script"

To nie doda use strict. Zobacz sourcetype option docs

Źródło: https://github.com/babel/babel/issues/7910#issuecomment-388517631

 3
Author: Yair Kukielka,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-07-17 12:53:44

Od wersji babel 6 możesz zainstalować najpierw babel-CLI (jeśli chcesz używać Babel z CLI ) lub babel-core (aby używać Node API). Pakiet ten nie zawiera modułów.

npm install --global babel-cli
# or
npm install --save-dev babel-core

Następnie zainstaluj moduły, których potrzebujesz. Więc nie instaluj modułu dla "trybu ścisłego" w Twoim przypadku.

npm install --save-dev babel-plugin-transform-es2015-arrow-functions

I dodać zainstalowane moduły w .plik babelrc wygląda tak:

{
  "plugins": ["transform-es2015-arrow-functions"]
}

Zobacz szczegóły tutaj: https://babeljs.io/blog/2015/10/31/setting-up-babel-6

 2
Author: Alina Poluykova,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-13 10:59:14

Dla babel 6 zamiast łatania predefiniowanego i/lub rozwidlania go i publikowania, możesz po prostu owinąć oryginalny plugin i ustawić opcję strict na false.

Coś w tym stylu powinno zadziałać:

const es2015preset = require('babel-preset-es2015');
const commonjsPlugin = require('babel-plugin-transform-es2015-modules-commonjs');

es2015preset.plugins.forEach(function(plugin) {
  if (plugin.length && plugin[0] === commonjsPlugin) {
    plugin[1].strict = false;
  }
});

module.exports = es2015preset;
 2
Author: Joscha,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-19 22:49:45

Proszę użyć "es2015-without-strict "zamiast " es2015". Nie zapomnij, że musisz mieć zainstalowany pakiet "babel-preset-es2015-without-strict". Wiem, że nie jest to oczekiwane domyślne zachowanie Babel, proszę wziąć pod uwagę, że projekt nie jest jeszcze dojrzały.

 2
Author: wandalen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-02 10:29:08

To nie jestgramatycznie poprawny, ale w zasadzie będzie działał zarówno dla Babel 5 jak i 6 bez konieczności instalowania modułu, który usuwa inny moduł.

code.replace(/^"use strict";$/, '')
 1
Author: Travis Webb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-20 18:43:13

Właśnie zrobiłem skrypt, który działa w węźle i usuwa "use strict" ; w wybranym pliku.

Plik: skrypt.js:

let fs = require('fs');
let file = 'custom/path/index.js';
let data = fs.readFileSync(file, 'utf8');
let regex = new RegExp('"use\\s+strict";');
if (data.match(regex)){
    let data2 = data.replace(regex, '');
    fs.writeFileSync(file, data2);
    console.log('use strict mode removed ...');
}
else {
    console.log('use strict mode is missing .');
}

node ./script.js

 0
Author: kolserdav,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-01 18:39:53

Jeśli używasz https://babeljs.io/repl (v7.8.6 w momencie pisania tego tekstu), możesz usunąć "use strict"; wybierając Source Type -> Module.

 0
Author: Abdull,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-03-05 11:02:12

Używanie wtyczek lub wyłączanie modułów i tryb ścisły, jak sugerowano w odpowiedzi @rcode nie zadziałało dla mnie.

Ale zmieniając cel z es2015|es6 do es5 w pliku tsconfig.json zgodnie z sugestią @andrefarzart w Ta odpowiedź na GitHub naprawiła problem.

// tsconfig.json file
{
  // ...
  "compilerOptions": {
    // ...
    "target": "es5", // instead of "es2015"
}
 0
Author: Wesley Gonçalves,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-11-11 02:24:13